/**
 * 网络请求
 */

//接口基类地址，测试开发环境
export const BASE_URL = "http://192.168.137.141:10083";
// export const IMAGE_BASE_URL = 'http://192.168.6.65:19083'
export const ASSET_URL = "http://192.168.6.65:10000"; // 静态资源地址（融合平台测试环境）
export const BASE_URL_YHT = "http://192.168.6.65:19001"; //融合平台测试环境
export const BASE_URL_A = "http://192.168.6.65:19023"; //融合平台测试环境
export const BASE_URL_S = "http://192.168.6.65:19073"; //融合平台测试环境
export const BASE_URL_FXDJ = "http://192.168.6.65:13333"; // 返乡登记地址(融合平台测试环境)

//接口基类地址，正式发版环境
// export const BASE_URL = 'https://www.yht.118114.cq.cn'
export const IMAGE_BASE_URL = "https://www.yht.118114.cq.cn";
// export const ASSET_URL = 'https://www.yht.118114.cq.cn'// 静态资源地址（ 融合平台正式环境）
// export const BASE_URL_A = 'https://www.yht.118114.cq.cn'// 融合平台正式环境
// export const BASE_URL_S = 'https://www.yht.118114.cq.cn'// 融合平台正式环境
// export const BASE_URL_FXDJ = 'https://www.yht.118114.cq.cn'// 返乡登记地址(融合平台正式环境)

export default (config, isToken = true) => {
  let { url } = config;
  if (!url.startsWith("/")) {
    url = "/" + url;
  }
  const token = uni.getStorageSync("gToken");

  // 定义请求头
  const header = Object.assign({}, config.header);
  console.log("header", header);
  header["dx-source"] = "APP";
  header["government-token"] =
    "a30ead4e96c58dc48dfb949a3c288d9d82d8366c2d271da1eea21b5b69cbb9aa18696a3c3a5f17ccea4d159a9843ce6b64123f994f9a640644bdb307085900cf18cea6b79151912f112f8f5be0a2a69a2fa1d8498a3569411756501b7007f181e7c696f9cd383158401b766ebfd1bb62";
  //
  // if (isToken) {
  // 	if (!token) {
  // 		// uni.showToast({
  // 		//   title: "您尚未登录，无法使用此功能！",
  // 		//   icon: "error",
  // 		// });
  // 		// return Promise.reject(Error("用户未登录"));
  // 	}
  // 	header['government-token'] = token || ''
  // }
  // console.info('请求-------', config)
  // //
  return new Promise((resolve, reject) => {
    uni.request({
      ...config,
      header: header,
      url: BASE_URL + url,
      success(res) {
        //实时存一下token
        let gToken = res.header["government-token"];
        if (gToken) {
          uni.setStorageSync("gToken", gToken);
        }

        //
        switch (res.statusCode) {
          case 404: {
            uni.showToast({
              title: res.data.message,
              icon: "error",
            });
            break;
          }
          case 500: {
            console.log("报500的请求错误返回：", res);
            //这几个代表token过期
            if (
              res.data.code == "002004" ||
              res.data.code == "002005" ||
              res.data.code == "002006" ||
              res.data.code == "002007"
            ) {
              //
              uni.setStorageSync("loginTag", "未登录");
              uni.removeStorageSync("token");
              uni.removeStorageSync("userData");
              uni.removeStorageSync("gToken");
              uni.setStorageSync("gToken", "visitor");
              //避免重复弹出
              if (uni.getStorageSync("showModalTag") != "已存在") {
                //
                uni.setStorageSync("showModalTag", "已存在");
                //
                uni.showModal({
                  title: "提示",
                  content: "您的登录已过期，请重新登录！",
                  confirmText: "去登录",
                  confirmColor: "#f63b1a",
                  cancelColor: "#081009",
                  success: (res) => {
                    if (res.confirm) {
                      uni.removeStorageSync("showModalTag");
                      //跳转到登录
                      uni.reLaunch({
                        url: "/pages/policy/policy",
                      });
                    } else {
                      uni.removeStorageSync("showModalTag");
                    }
                  },
                });
              }

              //其余的直接显示后台提示
            } else {
              uni.showToast({
                title: res.data.message,
                icon: "error",
              });
            }

            break;
          }
          case 401: {
            uni.showToast({
              title: res.data.message,
              icon: "error",
            });

            break;
          }
        }
        if (res.statusCode !== 200) {
          reject(res);
        }

        if (res.data && res.data.code == "-90") {
          uni.showToast({
            title: res.data.message,
            icon: "error",
          });
          uni.redirectTo({
            url: "/pages/policy/policy",
          });
          return;
        }
        //
        resolve(res.data);
      },

      fail(res) {
        console.info("响应fail-------", res);
        console.error(res);
        reject(res);
      },
    });
  });
};
